//
//  WZHEncryTool.swift
//  ShiGuangJiaSwift
//
//  Created by 汪志浩 on 2023/1/31.
//

import Foundation

public class WZHEncryTool: NSObject {

    static public func Encry(sb_pass:String , data_pass:String , sign_pass:String , parameters:[String:Any]) ->(header:[String:String] , jm_data:String , jm_sign:String){
        /// 这里把endpoint重新构造一遍主要为了解决网络请求地址里面含有? 时无法解析的bug https://github.com/Moya/Moya/issues/1198
        let req_id = UIDevice.UUID.md5 //创建一个唯一 id
        let req_time = "\(Date.getNowTimeTimestamp_sec())" //当前的时间戳
        let  identifierNumber =  UIDevice .current.keychainIdfv  //设备udid
        debugPrint("当前设备 ID--\(identifierNumber)")
        let app_ver = UIDevice.AppVersion
        let sb_lx = "ios"
        ///设置header
        var header = [String:String]()
        header.updateValue(sb_lx, forKey: "C-sb-lx") //设备类型
        header.updateValue("app", forKey: "C-sb-rlx") //请求端请类型
        header.updateValue(UIDevice.current.systemVersion, forKey: "C-sb-xt") //设备系统版本
        header.updateValue(UIDevice .current.systemName, forKey: "C-sb-jx")//设备机型
        header.updateValue("apple", forKey: "C-sb-pp")//设备品牌
        header.updateValue(app_ver, forKey: "C-sb-appver") //拾光家APP版本

        header.updateValue(req_id, forKey: "C-req-id")//请求ID
        header.updateValue(req_time, forKey: "C-req-time")//请求时间戳

        //计算加密的秘钥
        var sb_bs = ""
        let jmId_key = [sb_pass,req_id ,req_time].joined(separator: "*").md5
        if let sb_bs_jm = YLAESTool.AES_EncryptString(originString: identifierNumber, secret: jmId_key) as? String {
            sb_bs = sb_bs_jm
            header.updateValue(sb_bs, forKey: "C-sb-bs")
        }

        debugPrint("请求头内容--\(header)")
        
        var arr = [req_time ,app_ver, req_id ,identifierNumber,sign_pass,sb_lx]
//        var par = [String : Any]()
        var jm_data : String
        var jm_sign : String
        
        let jmdata_key = [data_pass,req_id ,req_time].joined(separator: "#").md5

        debugPrint("请求参数加密前--\(parameters)")

        if parameters.count > 0 ,let jm_dataJiaMi = YLAESTool.AES_EncryptDictionary(originDic: parameters, secret: jmdata_key) as? String {
            arr.insert(jm_dataJiaMi, at: 0)
//            par.updateValue(jm_data, forKey: "jm_data")
            jm_data = jm_dataJiaMi

            debugPrint("请求参数加密后--\(jm_data)")

        }else {
            arr.insert("", at: 0)
//            par.updateValue("", forKey: "jm_data")
            jm_data = ""
        }
        

        debugPrint("jm_sign加密前--\(arr)")
        let arrStr = arr.joined(separator: "*")
        jm_sign = arrStr.md5
        debugPrint("jm_sign加密后--\(jm_sign)")
//        par.updateValue(jm_sign, forKey: "jm_sign")
        return (header , jm_data , jm_sign)
        
    }
    
    static public func Decrypt(req_id:String , req_time:String ,pass:String ,data:String) ->String?{
        //计算解密的秘钥
//        let req_time = jm_data.req_time
        let app_ver =  UIDevice.AppVersion
//        let req_id = jm_data.req_id
        let sb_bs = UIDevice .current.keychainIdfv
        let sb_lx = "ios"
        let sin_arr = [req_time,app_ver,req_id,sb_bs,pass,sb_lx]
        let re_pass = sin_arr.joined(separator: "*#*").md5 //计算出来的秘钥
        guard let jm_data_jm = YLAESTool.AES_DecryptBase64(originData: data, secret: re_pass) as? String else {
//            handleErr("解密失败", failureCallback)
            return nil
        }
        
        return jm_data_jm
    }
}
